package com.gz.controller;

import com.gz.dto.QuestionDto;
import com.gz.entity.Question;
import com.gz.service.QuestionService;
import com.gz.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * (Question)表控制层
 *
 * @author makejava
 * @since 2024-02-19 16:30:39
 */
@RestController
@RequestMapping("question")
@Api(value = "题目控制层", tags = {"题目控制层"})
public class QuestionController {

  /**
   * 服务对象
   */
  @Autowired
  private QuestionService questionService;

  /**
   * 分页查询
   *
   * @param dto 筛选条件
   * @return 查询结果
   */
  @ApiOperation(value = "分页查询")
  @PostMapping("/queryPage")
  public R queryByPage(@RequestBody QuestionDto dto, HttpServletRequest request) {
    return R.ok(questionService.queryByPage(dto));
  }


  /**
   * 分页查询
   *
   * @param dto 筛选条件
   * @return 查询结果
   */
  @ApiOperation(value = "条件查询")
  @PostMapping("/queryAll")
  public R queryAll(@RequestBody QuestionDto dto, HttpServletRequest request) {
    return R.ok(questionService.queryAll(dto));
  }
  @ApiOperation(value = "错题辅助")
  @GetMapping("/queryByWrong")
  public R queryByWrong(@RequestParam("questionId") Integer questionId, HttpServletRequest request) {
    return R.ok(questionService.queryByWrong(questionId));
  }

  /**
   * 通过主键查询单条数据
   *
   * @param id 主键
   * @return 单条数据
   */
  @ApiOperation(value = "主键查询")
  @GetMapping("/queryById")
  public R<Question> queryById(@RequestParam("id") Integer id) {
    return R.ok(questionService.queryById(id));
  }

  /**
   * 新增数据
   *
   * @param question 实体
   * @return 新增结果
   */
  @ApiOperation(value = "新增数据")
  @PostMapping("/save")
  public R<Question> add(@RequestBody Question question) {
    if(CollectionUtils.isEmpty(question.getQuestionLabelIds())){
      return R.failure("题目标签不能为空");
    }
    return R.ok(questionService.insert(question));
  }

  /**
   * 编辑数据
   *
   * @param question 实体
   * @return 编辑结果
   */
  @ApiOperation(value = "编辑数据")
  @PostMapping("/update")
  public R<Question> edit(@RequestBody Question question) {
    return R.ok(questionService.update(question));
  }

  /**
   * 批量编辑数据
   *
   * @param list 实体
   * @return 编辑结果
   */
  @ApiOperation(value = "批量编辑数据")
  @PostMapping("/updateBatch")
  public R<?> editBatch(@RequestBody List<Question> list) {
    return R.ok(questionService.updateBatch(list));
  }

  /**
   * 删除数据
   *
   * @param id 主键
   * @return 删除是否成功
   */
  @GetMapping("/deleteById")
  @ApiOperation(value = "删除数据")
  public R<Boolean> deleteById(@RequestParam("id") Integer id) {
    return R.ok(questionService.deleteById(id));
  }

}

